package org.kardigen.lg.admin.client.training;

import java.util.List;

import org.kardigen.lg.admin.client.training.event.ChapterSelectionEvent;
import org.kardigen.lg.admin.client.training.event.TrainingSelectionEvent;
import org.kardigen.lg.admin.client.training.event.WordSelectionEvent;
import org.kardigen.lg.service.client.proxy.TrainingChapterProxy;
import org.kardigen.lg.service.client.proxy.TrainingProxy;
import org.kardigen.lg.service.client.proxy.WordProxy;
import org.kardigen.lg.service.client.requestfactory.TrainingRequestContext;
import org.kardigen.lg.service.client.requestfactory.TrainingRequestFactory;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class TrainingsViewPresenter implements TrainingsViewDisplay.Presenter {

	private final TrainingRequestFactory requestFactory;
	private final TrainingsViewDisplay display;
	private EventBus eventBus;
	private SelectionHandler<TreeItem> treeSelectionHandler;

	public TrainingsViewPresenter(TrainingsViewDisplay display) {
		this.display = display;
		requestFactory = GWT.create(TrainingRequestFactory.class);
	}

	public void start(EventBus eventBus) {
		this.eventBus = eventBus;
		requestFactory.initialize(eventBus);
		display.setPresenter(this);
		getAndShowTrainings();
	}

	private void getAndShowTrainings() {
		TrainingRequestContext requestContext = requestFactory.createTrainingRequestContext();
		requestContext.findAllTrainings()
		.with("chapters","chapters.words","chapters.words.image")
		.fire(new Receiver<List<TrainingProxy>>() {

			@Override
			public void onSuccess(List<TrainingProxy> response) {
					showTrainings(response);
			}
		});

	}

	protected void showTrainings(List<TrainingProxy> trainingsProxies) {
		Tree traingsTree = display.getTraingsTree();
		traingsTree.clear();
		TreeItem root = traingsTree.addItem("USER");
		addTrainigs(trainingsProxies, root);
	}

	private void addTrainigs(List<TrainingProxy> trainingsProxies, TreeItem root) {
	   for (TrainingProxy trainingProxy : trainingsProxies) {
			TreeItem trainingItem = root.addItem(trainingProxy.getTrainingName());
			trainingItem.setUserObject(trainingProxy);
			addChapters(trainingItem, trainingProxy.getChapters());
		}
   }

	private void addChapters(TreeItem trainingItem, List<TrainingChapterProxy> chapters) {
		if (chapters != null) {
			for (TrainingChapterProxy chapterProxy : chapters) {
				TreeItem chapterItem = trainingItem.addItem(chapterProxy.getChapterName());
				chapterItem.setUserObject(chapterProxy);
				addWords(chapterItem, chapterProxy.getWords());
			}
		}
	}

	private void addWords(TreeItem chapterItem, List<WordProxy> words) {
		if (words != null) {
			for (WordProxy wordProxy : words) {
				TreeItem wordItem = chapterItem.addItem(wordProxy.getWord());
				wordItem.setUserObject(wordProxy);
			}
		}
	}

	public void onTreeSelectionAction(TreeItem item) {
		Object userObject = item.getUserObject();
		TreeItem parentItem = item.getParentItem();

		Object parentUserObject = null;
		if (parentItem != null) {
			parentUserObject = parentItem.getUserObject();
		}

		if (userObject == null) {
			eventBus.fireEvent(new TrainingSelectionEvent(null));
		} else {

			if (userObject instanceof TrainingProxy) {
				eventBus.fireEvent(new TrainingSelectionEvent((TrainingProxy) userObject));

			} else if (userObject instanceof TrainingChapterProxy) {
				eventBus.fireEvent(new ChapterSelectionEvent((TrainingProxy) parentUserObject,
				      (TrainingChapterProxy) userObject));

			} else if (userObject instanceof WordProxy) {
				eventBus.fireEvent(new WordSelectionEvent((TrainingProxy) parentItem.getParentItem().getUserObject(),
				      (TrainingChapterProxy) parentUserObject, (WordProxy) userObject));
			}
		}
	}

}
